package com.android.delivery.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.android.delivery.App;
import com.android.delivery.MainActivity;
import com.android.delivery.R;
import com.android.delivery.api.ApiService;
import com.android.delivery.base.BaseActivity;
import com.android.delivery.callback.DialogCallback;
import com.android.delivery.callback.LzyResponse;
import com.android.delivery.entity.LoginInfo;
import com.android.delivery.util.ToastUtils;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by mqwang on 2017/9/15.
 */

public class LoginActivity extends BaseActivity {

    @BindView(R.id.phone_et)
    EditText phoneEt;
    @BindView(R.id.pwd_et)
    EditText pwdEt;
    @BindView(R.id.tv_code)
    TextView sendCodeTv;
//    @BindView(R.id.pwd_visible_iv)
//    ImageView pwdVisibleIv;

    private Handler mHandler = new Handler();
    private int expired_time = 60;

    public static void self(Context context){
        Intent intent = new Intent(context, LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void init(Bundle savedInstanceState, View container) {
        LogUtils.d("hello");
        BarUtils.setStatusBarColor(this, getResources().getColor(R.color.colorWhite));

    }

//    @Override
//    protected boolean isChangeStatusBarColor() {
//        return true;
//    }

    @Override
    protected boolean isNeedTitleBar() {
        return false;
    }

    @OnClick({R.id.login_tv, R.id.tv_code/*, R.id.register_tv, R.id.forget_tv, R.id.pwd_visible_iv*/})
    public void onViewClicked(View view) {
        String phone = phoneEt.getText().toString().trim();

        switch (view.getId()) {
            case R.id.login_tv:
                String pwd = pwdEt.getText().toString().trim();
                if (ObjectUtils.isEmpty(phone)){
                    showToast("请输入用户名");
                    return;
                }
                if (ObjectUtils.isEmpty(pwd)){
                    showToast("请输入密码");
                    return;
                }

                ApiService.login(this, phone, pwd, new DialogCallback<LzyResponse<LoginInfo>>(this) {
                    @Override
                    public void onError(Response<LzyResponse<LoginInfo>> response) {
                        super.onError(response);
                        ToastUtils.showShort(LoginActivity.this, response.message());
                    }

                    @Override
                    public void onSuccess(Response<LzyResponse<LoginInfo>> response) {
                        LoginInfo loginInfo = response.body().data;
                        if (loginInfo != null){
                            App.Companion.getInstance().setLoginInfo(loginInfo);
                            ActivityUtils.startActivity(MainActivity.class);
                        }
                    }
                });
                break;
            case R.id.tv_code:
                if (TextUtils.isEmpty(phone)) {
                    showToast("请输入手机号码");
                    return;
                }

                ApiService.verifycode(this, phone, new DialogCallback<LzyResponse<Object>>(this) {
                    @Override
                    public void onSuccess(Response<LzyResponse<Object>> response) {
                        sendCodeTv.setClickable(false);
                        sendCodeTv.setText(expired_time + "秒");
                        mHandler.postDelayed(runnable, 1000);
                    }

                    @Override
                    public void onError(Response<LzyResponse<Object>> response) {
                        super.onError(response);

                        ToastUtils.showShort(LoginActivity.this, response.message());
                    }
                });
                break;
//            case R.id.register_tv:
//                ActivityUtils.startActivity(this, RegisterStepPhoneActivity.class);
//                break;
//            case R.id.forget_tv:
//                ActivityUtils.startActivity(this, MainActivity.class);
//                break;
//            case R.id.pwd_visible_iv:
//                if (isVisible){
//                    pwdVisibleIv.setImageResource(R.mipmap.icon_login_eyes_normal);
//                    isVisible = false;
//                    pwdEt.setTransformationMethod(PasswordTransformationMethod.getInstance());
//                }else {
//                    pwdVisibleIv.setImageResource(R.mipmap.icon_login_eyes_click);
//                    isVisible = true;
//                    pwdEt.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
//                }
//                break;
//                default:
        }
    }

    /**
     * 60秒倒计时
     */
    private Runnable runnable = new Runnable() {

        @Override
        public void run() {
            expired_time--;
            sendCodeTv.setText(expired_time + "秒");
            if (expired_time == 0) {
                initVcode();
                return;
            }
            mHandler.postDelayed(this,1000);
        }
    };

    /*
     * 60秒过后初始化各状态
     */
    private void initVcode(){
        sendCodeTv.setClickable(true);
        sendCodeTv.setText("发送验证码");
    }

}
